package yhao.infra.web.interceptor.logger.context;


/**
 * 
 * @author Administrator
 * @version $Id: DigestContextHolder.java, v 0.1 2015年4月30日 上午10:06:51 Administrator Exp $
 */
public class RequestContextHolder {

    private static final ThreadLocal<RequestContext> digestContextHolder
            = new ThreadLocal<>();


    public static <T extends RequestContext> void init(String appName, Class<T> clazz) {
        RequestContext ctx = digestContextHolder.get();
        if (ctx == null) {
            try {
                ctx = clazz.getDeclaredConstructor(new Class[]{}).newInstance();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            if(appName!=null){
                ctx.setAppName(appName);
            }
            digestContextHolder.set(ctx);
        }
    }

    public static <T extends RequestContext> T getDigestContext() {
        return (T)digestContextHolder.get();
    }

    public static void clear() {
        digestContextHolder.remove();
    }
}